/*---------------页面初始化时列表跳至最底bug解决-------------------*/
window.onload = function(){
	$('.mui-scroll').css('transform','translate3d(0px,0px,0px) !important');
	setTimeout(function(){
		$('.mui-scroll').css('transform','translate3d(0px,0px,0px)');
	},0);
}

var orderBy = null;
var categoryId = null;
//类目筛选
$('.leftShaiXuan').on('click',function(){
	if($(this).attr("hide") == "true"){
		$('.firstCategory').show();
		$('#secondPopover').hide();
		$(this).attr("hide","false")
		$('.rightShaiXuan').attr("hide","true")
	}else{
		$('.firstCategory').hide();
		$(this).attr("hide","true")
	}
})

//排序
$('.rightShaiXuan').on('click',function(){
	if($(this).attr("hide") == "true"){
		$('#secondPopover').show();
		$('.firstCategory').hide();
		$('.leftShaiXuan').attr("hide","true")
		$(this).attr("hide","false")
	}else{
		$('#secondPopover').hide();
		$(this).attr("hide","true")
	}
})

//商品类目筛选后商品列表
$('.firstCategory').find('li').on('click',function(){
	$(this).find('ul').show();
	$(this).siblings().find('ul').hide();
})

$('.firstCategory').find('li ul li').on('click',function(){
	var _this = $(this);
	categoryId = _this.attr("categoryId");
	$('.leftShaiXuan').find('b').html($(this).html());
	$('.leftShaiXuan').find('b').attr('categoryId',categoryId);
	
	orderBy = $('.rightShaiXuan').find('b').attr('orderBy');
	$('#secondPopover').hide();
	$('.rightShaiXuan').attr("hide","true");
	$('.leftShaiXuan').attr("hide","true");
	categoryId = $(this).attr("categoryId");
	$.ajax({
		type:"POST",
		url : "/weishang/distributor/distributorItemListMore.htm",
		dataType : "html",
		data : {
			"orderBy" :orderBy,
			"categoryId" :categoryId
		},
		success : function(data) {
			$('.ulWrap').html(data);
			_this.parent().parent().parent().hide();
			_this.parent().hide();
			
	}
	});
})
//不选类目
$('.allItem').on('click',function(){
	var _this = $(this);
	$('.leftShaiXuan').find('b').html($(this).html());
	$('.leftShaiXuan').find('b').attr('categoryId',null);
	orderBy = $('.rightShaiXuan').find('b').attr('orderBy');
	$('#secondPopover').hide();
	$('.firstCategory').hide();
	$('.rightShaiXuan').attr("hide","true");
	$('.leftShaiXuan').attr("hide","true");
	$.ajax({
		type:"POST",
		url : "/weishang/distributor/distributorItemListMore.htm",
		dataType : "html",
		data : {
			"orderBy" :orderBy,
			"categoryId" :null
		},
		success : function(data) {
			$('.ulWrap').html(data);
	}
	});
})

//排序后列表
$('#secondPopover').find('ul li').on('click',function(){
  	var _this = $(this);
  	orderBy = _this.attr('orderBy');
	$('.rightShaiXuan').find('b').html($(this).html());
	$('.rightShaiXuan').find('b').attr('orderBy',orderBy);
	$('.rightShaiXuan').attr("hide","true");
	$('#secondPopover').hide();
	$('.leftShaiXuan').attr("hide","true");
	$('.firstCategory').hide();
	categoryId = $('.leftShaiXuan').find('b').attr('categoryId');;
	$.ajax({
		type:"POST",
		url : "/weishang/distributor/distributorItemListMore.htm",
		dataType : "html",
		data : {
			"orderBy" :orderBy,
			"categoryId" :categoryId
		},
		success : function(data) {
			$('.ulWrap').html(data);
		}
	})
})

//加入购物车
mui('body').on('tap','.addShopTo',function(e){
	e.preventDefault();
	var mallItemId = $(this).attr('mallItemId');//shopId to ajax post
    $(this).find('img').attr('src',$(this).find('img').attr('data-src'));
    $.ajax({
    	type:"POST",
		url : "/weishang/distributor/itemCollection.json",
		dataType : "json",
		data : {
			"mallItemId" :mallItemId
		},
		success : function(data) {
			window.location.href="/weishang/distributor/distributorItemList.htm";
		}
    });
})

$('.addShopIcon').on('click',function(e){
	e.preventDefault();
})

/*---------------------------上拉加载更多-------------------------------*/
/*$('.clickMore').on('click',function(){
	alert(1);
	$.ajax({
		url:'/weishang/distributor/distributorItemListMore.htm',
		type:'get',
		dataType:'html',
		data:{
			'orderBy':orderBy,
			'categoryId':categoryId,
			'pageNo':$('#pageNum').val()
		},
		success:function(data){
			console.log(data);
			if(!(data.replace(/(^\s*)|(\s*$)/g,"") == '')){
				$('#pageNum').val($('#pageNum').val()-(-1));
				my.innerHTML = data;
				ul.appendChild(my);
			}else{
				dataStatus = true;
				loadMore.innerHTML = '没有更多数据了';
				}
			}
		});
})*/
mui.init({
	  pullRefresh : {
	    container:'#pullrefresh',//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
up : {
  height:50,//可选.默认50.触发上拉加载拖动距离
  auto:true,//可选,默认false.自动上拉加载一次
  contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
  contentnomore:'没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
  callback: function() {
			var self = this;
			var dataStatus = false;
			var index = document.querySelector('.mui-table-view').getAttribute('index');
			var numObj = document.querySelector('#pageNum');
			var loadMore = document.querySelector('.mui-pull-loading');
				var ul = document.querySelector('.ulWrap');
				var my = document.createElement('my');
				$.ajax({
					url:'/weishang/distributor/distributorItemListMore.htm',
					type:'get',
					dataType:'html',
					data:{
						'orderBy':orderBy,
						'categoryId':categoryId,
						'pageNo':numObj.value
					},
					success:function(data){
						if(!(data.replace(/(^\s*)|(\s*$)/g,"") == '')){
							numObj.value = numObj.value -(-1);
							my.innerHTML = data;
							ul.appendChild(my);
						}else{
							dataStatus = true;
							loadMore.innerHTML = '没有更多数据了';
							}
						}
					});
					self.endPullupToRefresh(dataStatus);
			}
    }
  }
});